package com.huabo.contract.mysql.mapper;

import com.hbfk.util.DateUtil;
import com.hbfk.util.PageInfo;
import com.huabo.contract.mysql.entity.TblContractSpnodeMySql;

public class TblContractSpnodeMySqlMapperSqlConfig {

	public String updateBySpnode(TblContractSpnodeMySql spNode) {
		StringBuffer sql = new StringBuffer("UPDATE TBL_CONTRACT_SPNODE SET NODEID = '"+spNode.getNodeid()+"'");

		if(spNode.getNodestatus() != null && !"".equals(spNode.getNodestatus())) {
			sql.append(" ,NODESTATUS = '"+spNode.getNodestatus()+"'");
		}
		if(spNode.getNodememo() != null && !"".equals(spNode.getNodememo())) {
			sql.append(" ,NODEMEMO = '"+spNode.getNodememo()+"'");
		}
		if(spNode.getNodecontent() != null && !"".equals(spNode.getNodecontent())) {
			sql.append(" ,NODECONTENT = '"+spNode.getNodecontent()+"'");
		}
		if(spNode.getNodemoney() != null) {
			sql.append(" ,NODEMONEY = '"+spNode.getNodemoney()+"'");
		}
		if(spNode.getFormid() != null) {
			sql.append(" ,FORMID = '"+spNode.getFormid()+"'");
		}
		if(spNode.getContractid() != null) {
			sql.append(" ,CONTRACTID = '"+spNode.getContractid()+"'");
		}
		if(spNode.getNodepost() != null && !"".equals(spNode.getNodepost())) {
			sql.append(" ,NODEPOST = '"+spNode.getNodepost()+"'");
		}
		if(spNode.getIswy() != null && !"".equals(spNode.getIswy())) {
			sql.append(" ,ISWY = '"+spNode.getIswy()+"'");
		}
		if(spNode.getStartdate() != null) {
			sql.append(" ,STARTDATE = TO_DATE('"+DateUtil.parseDate(spNode.getStartdate(),"yyyy-MM-dd HH:mm:ss") +"', 'YYYY-MM-DD HH24:MI:SS')");
		}
		if(spNode.getEnddate() != null) {
			sql.append(" ,ENDDATE = TO_DATE('"+DateUtil.parseDate(spNode.getEnddate(),"yyyy-MM-dd HH:mm:ss") +"', 'YYYY-MM-DD HH24:MI:SS')");
		}
		if(spNode.getNodefinishdate() != null) {
			sql.append(" ,NODEFINISHDATE = TO_DATE('"+DateUtil.parseDate(spNode.getNodefinishdate(),"yyyy-MM-dd HH:mm:ss") +"', 'YYYY-MM-DD HH24:MI:SS')");
		}
		if(spNode.getNodeplanfinishdate() != null) {
			sql.append(" ,NODEPLANFINISHDATE = TO_DATE('"+DateUtil.parseDate(spNode.getNodeplanfinishdate(),"yyyy-MM-dd HH:mm:ss") +"', 'YYYY-MM-DD HH24:MI:SS')");
		}
		sql.append(" WHERE SPNODEID = '"+spNode.getSpnodeid()+"'");
		return sql.toString();
	}

	public String saveBySpnode(TblContractSpnodeMySql spNode) {
		StringBuffer column = new StringBuffer("INSERT INTO TBL_CONTRACT_SPNODE (SPNODEID");
		StringBuffer value = new StringBuffer(" VALUES (HIBERNATE_SEQUENCE.nextval");

		if(spNode.getNodeid() != null) {
			column.append(",NODEID");
			value.append(",'"+spNode.getNodeid()+"'");
		}
		if(spNode.getNodestatus() != null && !"".equals(spNode.getNodestatus())) {
			column.append(",NODESTATUS");
			value.append(",'"+spNode.getNodestatus()+"'");
		}
		if(spNode.getNodememo() != null && !"".equals(spNode.getNodememo())) {
			column.append(",NODEMEMO");
			value.append(",'"+spNode.getNodememo()+"'");
		}
		if(spNode.getNodecontent() != null && !"".equals(spNode.getNodecontent())) {
			column.append(",NODECONTENT");
			value.append(",'"+spNode.getNodecontent()+"'");
		}
		if(spNode.getNodemoney() != null) {
			column.append(",NODEMONEY");
			value.append(",'"+spNode.getNodemoney()+"'");
		}
		if(spNode.getFormid() != null) {
			column.append(",FORMID");
			value.append(",'"+spNode.getFormid()+"'");
		}
		if(spNode.getContractid() != null) {
			column.append(",CONTRACTID");
			value.append(",'"+spNode.getContractid()+"'");
		}
		if(spNode.getNodepost() != null && !"".equals(spNode.getNodepost())) {
			column.append(",NODEPOST");
			value.append(",'"+spNode.getNodepost()+"'");
		}
		if(spNode.getIswy() != null && !"".equals(spNode.getIswy())) {
			column.append(",ISWY");
			value.append(",'"+spNode.getIswy()+"'");
		}
		if(spNode.getStartdate() != null) {
			column.append(",STARTDATE");
			value.append(",TO_DATE('"+ DateUtil.parseDate(spNode.getStartdate(),"yyyy-MM-dd HH:mm:ss") +"', 'YYYY-MM-DD HH24:MI:SS')");
		}
		if(spNode.getEnddate() != null) {
			column.append(",ENDDATE");
			value.append(",TO_DATE('"+DateUtil.parseDate(spNode.getEnddate(),"yyyy-MM-dd HH:mm:ss") +"', 'YYYY-MM-DD HH24:MI:SS')");
		}
		if(spNode.getNodefinishdate() != null) {
			column.append(",NODEFINISHDATE");
			value.append(",TO_DATE('"+DateUtil.parseDate(spNode.getNodefinishdate(),"yyyy-MM-dd HH:mm:ss") +"', 'YYYY-MM-DD HH24:MI:SS')");
		}
		if(spNode.getNodeplanfinishdate() != null) {
			column.append(",NODEPLANFINISHDATE");
			value.append(",TO_DATE('"+DateUtil.parseDate(spNode.getNodeplanfinishdate(),"yyyy-MM-dd HH:mm:ss") +"', 'YYYY-MM-DD HH24:MI:SS')");
		}
		column.append(")");
		value.append(")");
		String sql = column.toString()+value.toString();
		return sql;
	}


	public String findListByXdf(PageInfo<TblContractSpnodeMySql> pageInfo, String budgetid) throws Exception {
		StringBuffer sqlSb = new StringBuffer("SELECT * FROM (SELECT BUDGET.*,ROWNUM RN FROM (SELECT * FROM TBL_CONTRACT_SPNODE TCS\n" +
				"LEFT JOIN TBL_CYHW_UNIT TCU ON TCS.CONTRACTID = TCU.CONTRACTID\n" +
				"WHERE TCS.ISWY='是' AND TCS.NODEPOST IS NOT NULL AND TCS.CONTRACTID IN (SELECT CONTRACTID FROM TBL_CYHW_UNIT WHERE CONTRACTXDFXINFO=" + budgetid);

		sqlSb.append(" )) BUDGET WHERE rownum <= "+(pageInfo.getCurrentPage()*pageInfo.getPageSize())+" ) WHERE RN > "+pageInfo.getCurrentRecord());
		String sql = sqlSb.toString();
		return sql;
	}

	public String findListByXdfCount(String budgetid) throws Exception {
		StringBuffer sqlSb = new StringBuffer("SELECT COUNT(*) FROM TBL_CONTRACT_SPNODE TCS\n" +
				"LEFT JOIN TBL_CYHW_UNIT TCU ON TCS.CONTRACTID = TCU.CONTRACTID\n" +
				"WHERE TCS.ISWY='是' AND TCS.NODEPOST IS NOT NULL AND TCS.CONTRACTID IN (SELECT CONTRACTID FROM TBL_CYHW_UNIT WHERE CONTRACTXDFXINFO=" + budgetid+")");

		//sqlSb.append(" ) BUDGET WHERE rownum <= "+(pageInfo.getCurrentPage()*pageInfo.getPageSize())+" ) WHERE RN > "+pageInfo.getCurrentRecord());
		String sql = sqlSb.toString();
		return sql;
	}
}
